package com.springboot3.test.springboot3testdemo4.demos.web.config;

import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.zalando.logbook.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2024/9/12-10:10
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Configuration
public class LogbookConfig {
  @Bean
  Logbook logbook() {
    return Logbook.builder()
        .sink(
            new Sink() {
              private static final Logger LOGGER = LoggerFactory.getLogger("logbook");

              /**
               * @param precorrelation
               * @param request
               * @throws IOException
               */
              @Override
              public void write(Precorrelation precorrelation, HttpRequest request)
                  throws IOException {}

              /**
               * @param correlation
               * @param request
               * @param response
               * @throws IOException
               */
              @Override
              public void write(Correlation correlation, HttpRequest request, HttpResponse response)
                  throws IOException {
                LOGGER.info("==============================");
                LOGGER.info("request header:\t" + request.getHeaders());
                LOGGER.info("request body:\t" + request.getBodyAsString());
                LOGGER.info("\n");
                LOGGER.info("response header:\t" + response.getHeaders());
                LOGGER.info("response body:\t" + response.getBodyAsString());
                LOGGER.info("==============================");
              }
            })
        .build();
  }
}
